package gitee.cheungming.modules.governance.config;

import com.taosdata.jdbc.TSDBDriver;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

@Configuration
public class TDengineConfig {

    @Bean(name = "tdengineDataSource")
    public HikariDataSource tdengineDataSource() {
        HikariConfig config = new HikariConfig();
        String jdbcUrl = "jdbc:TAOS-WS://localhost:6041?user=root&password=taosdata";
        config.setJdbcUrl(jdbcUrl);
        config.setMinimumIdle(10);
        config.setMaximumPoolSize(10);
        config.setConnectionTimeout(30000);
        config.setMaxLifetime(0);
        config.setIdleTimeout(0);
        config.setConnectionTestQuery("SELECT 1");
        config.setDataSourceProperties(properties());
        return new HikariDataSource(config);
    }


    @Bean(name = "tdengineProperties")
    public Properties properties() {
        Properties properties = new Properties();
        properties.setProperty(TSDBDriver.PROPERTY_KEY_ASYNC_WRITE, "stmt");
        properties.setProperty(TSDBDriver.PROPERTY_KEY_BATCH_SIZE_BY_ROW, "5000");
        properties.setProperty(TSDBDriver.PROPERTY_KEY_CACHE_SIZE_BY_ROW, "10000");
        properties.setProperty(TSDBDriver.PROPERTY_KEY_BACKEND_WRITE_THREAD_NUM, "10");
        properties.setProperty(TSDBDriver.PROPERTY_KEY_ENABLE_AUTO_RECONNECT, "true");
        properties.setProperty(TSDBDriver.PROPERTY_KEY_MESSAGE_WAIT_TIMEOUT, "5000");
        properties.setProperty(TSDBDriver.PROPERTY_KEY_COPY_DATA, "false");
        properties.setProperty(TSDBDriver.PROPERTY_KEY_STRICT_CHECK, "false");
        return properties;
    }
}
